<?php
/**
 * Copyright (c) 2009, Microsoft
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Microsoft nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Microsoft ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Microsoft BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   Microsoft
 * @package    WebErrorPHPToolkit
 * @subpackage Support
 * @version    $Id: SupportFunctions.php, v 1.0 2009-10-09 08:00:00Z unknown $
 * @copyright  Copyright (c) 2009, Microsoft (http://www.microsoft.com)
 * @author     a-jetann   
 * 
 * Holds variables that used by Custom 404 page, primarily 
 * configuration info.
 */

/**
 * Write HTML hidden variable upon web page.
 * @param $config_name
 * @param $value
 * @return unknown_type
 */
function web_error_set_hidden($config_name, $value) {
  $config_name = trim($config_name);
  $value = trim($value);
  print '<input type="hidden" id="'.$config_name.'" name="'.$config_name.'" value="'.$value.'" />'."\n";
}

/**
 * Validate whether a string is valid UTF-8.
 * @param $text
 * @return boolean
 */
function string_check_utf8($text) {
  if (strlen($text) == 0) {
    return TRUE;
  }

  return (preg_match('/^./us', $text) == 1);
}

/**
 * Add special characters in a plain-text string for display as HTML.
 * @param string $text
 * @return string
 */
function string_check_plain($text) {
  return string_check_utf8($text) ? htmlspecialchars($text, ENT_QUOTES) : '';
}



/**
 * case insensitive version of in_array()
 *
 * @param string $needle
 * @param string $haystack
 * @return boolean unknown_type
 */
function in_arrayi( $needle, $haystack ) {
  $found = false;
  foreach( $haystack as $value ) {
    if( strtolower( $value ) == strtolower( $needle ) ) {
        $found = true;
    }
  }
  return $found;
}


/**
 * Determine if extension within URI is in array of extensions.
 * @param string $strURI
 * @param array strings $extension_array
 * @return boolean
 */
function in_extension_array( $strURI, $extensions_array, &$match_found )
{
  /*
   * Regex pattern:
   * a. Find extension between period(.) and question-mark(?)
   * OR
   * b. Find extension between period(.) and end-of-line($)
   *
   * Tested using:
   * http://www.spaweditor.com/scripts/regex/index.php
   */
  if (preg_match('/((\.([^\.]+)\?)|(\.([^\.]+)$))/', $strURI, $matches))
  {
    /*
     * Find not empty item in matches array
     */
    foreach($matches as $match)
    {
      if (!empty($match))
      {
        break;
      }
    }

    /*
     * Clean-up match: Trim off spaces, question-mark(?) and period(.)
     */
    $match = trim($match, " \?\.");

    /*
     * Is clean-up match within extension array?
     */
    if (in_arrayi($match, $extensions_array))
    {
      $match_found = $match;
      return true;
    }
  }

  return false;
}

/**
 * if string value is in array. Case-insensitive compare.
 * @param string $str
 * @param array $a
 * @return boolean
 */
function sting_in_iarray($str, $a)
{
  foreach($a as $v)
  {
    if(strcasecmp($str, $v)==0)
    {
    	return true;
    }
  }
  return false;
}

/**
 * Takes an input array and returns a new array without duplicate values with Case-insensitive compare.
 * @param array $a
 * @return array
 */
function string_array_iunque($a)
{
  $n = array();
  foreach($a as $k=>$v)
  {
    if(!sting_in_iarray($v, $n))
    {
    	$n[$k]=$v;
    }
  }
  return $n;
}

/*
 * Helper function for performing a reverse
 * sorting array by string length.
 */
function string_reverse_sort_by_length_functor($val_1, $val_2)
{
  // initialize the return value to zero
  $retVal = 0;

  // compare lengths
  $firstVal = strlen($val_1);
  $secondVal = strlen($val_2);

  if($firstVal < $secondVal)
  {
    $retVal = 1;
  }
  else if($firstVal > $secondVal)
  {
    $retVal = -1;
  }
  return $retVal;
}




/**
 * Apply a prefix to an item.
 * @param $item1
 * @param $key
 * @param $prefix
 */
function web_error_key_alter_functor(&$item1, $key, $prefix)

{
  $item1 = "$prefix:$item1";
}


/**
 * Apply extension before and after item.
 * @param $item1
 * @param $key
 * @param $extension
 * @return void
 */
function web_error_key_extension_functor(&$item1, $key, $extension)
{
  $item1 = "{$extension}.{$item1}{$extension}i";
}

?>